package com.breaktian.share.action;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;

import com.breaktian.share.Config;
import com.breaktian.share.Share;
import com.breaktian.share.ShareChannel;
import com.breaktian.share.ShareEntity;
import com.breaktian.share.Utils;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.List;

public class WeixinAction implements ShareAction {
    private final Context mContext;
    private final IWXAPI api;
    private Share mShare;

    public WeixinAction(Context context) {
        mContext = context;
        api = WXAPIFactory.createWXAPI(context, Config.WX_APP_ID, true);
        api.registerApp(Config.WX_APP_ID);
    }

    @Override
    public boolean isAppInstalled() {
        if(api.isWXAppInstalled() && api.isWXAppSupportAPI()) {
            return true;
        } else {
            final PackageManager packageManager = mContext.getPackageManager();
            List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
            if (pinfo != null) {
                for (int i = 0; i < pinfo.size(); i++) {
                    String pn = pinfo.get(i).packageName;
                    if (pn.equalsIgnoreCase("com.tencent.mm")) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    @Override
    public void share(ShareEntity entry, ShareChannel channel) {
        mShare = Share.Registrar.getInstance().getShare();
        if(entry!=null){
            if(entry.isImageShare()){
                shareImage(entry,channel);
            }else{
                shareWebPage(entry,channel);
            }
        }
    }

    /**
     * 分享链接
     * 朋友圈：
     * webpageUrl必填
     * 微信好友:
     * webpageUrl 必填
     * title必填
     *
     */
    void shareWebPage(ShareEntity entry, ShareChannel channel){
        WXWebpageObject wxWebpageObject = new WXWebpageObject();
        if (!TextUtils.isEmpty(entry.url)) {
            wxWebpageObject.webpageUrl = entry.url;
        }else{
            mShare.failure("WebPage分享链接不得为空");
        }
        WXMediaMessage msg = new WXMediaMessage(wxWebpageObject);
        msg.title = !TextUtils.isEmpty(entry.title) ? entry.title : "";
        msg.description = !TextUtils.isEmpty(entry.content) ? entry.content : "";
        msg.thumbData = Utils.compressImage(entry.imagePath, 32 * 1024);
        //构造Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = ShareChannel.SESSION == channel ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);

    }

    /**
     * 分享图片
     */
    void shareImage(ShareEntity entry, ShareChannel channel){
        Bitmap bitmap = BitmapFactory.decodeFile(entry.imagePath);
        WXImageObject imageObject = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imageObject;
        msg.thumbData = Utils.compressImage(entry.imagePath, 32 * 1024);
        //构造Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = ShareChannel.SESSION == channel ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }
}
